#This is an example of how to embeebed RF+PAM in a script
from RFPAM import Unsupervised_Random_Forest
import numpy as np
import time

def run_example(dataset='data/usecase.new'):
    """ Trains the forest and predicts a new observation (random from the dataset) every 3 seconds
    """
    rfpam = Unsupervised_Random_Forest()
    training_set = rfpam.read_data_from_file(dataset)
    number_of_clusters = 8
    number_of_trees = 50
    print 'Training Forest'
    forest, medoids_leaves_trees_results = rfpam.pam_online_training(training_set, number_of_clusters, number_of_trees)
    while True:
        index_array = np.arange(training_set.shape[0]) #create an array with the number of the row
        np.random.shuffle(index_array)
        observations = training_set[index_array[:1]]
        print '\nClustering new observation: \n', observations
        cluster_assignments = rfpam.pam_online_clustering(forest, medoids_leaves_trees_results, observations) 
        print '\nCluster of the new Observation: \n', cluster_assignments
        time.sleep(3)

run_example()
